package book.zone.helpers;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * User: NDemeshchenko
 * Date: May 18, 2010
 * Time: 12:09:57 PM
 */
public class PasswordCrypt {

    public static synchronized String encrypt(String s){
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        try{
            if (md != null) {
                md.update(s.getBytes("UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte raw[]= new byte[0];
        if (md != null) {
            raw = md.digest();
        }
        return (new BASE64Encoder()).encode(raw);
    }
}
